package leetcode_acm.array;

import org.junit.Test;

/**
 * 功能描述
 *
 * @author: 张庭杰
 * @date: 2023年03月17日 15:17
 */
public class MySqrt {
    public int mySqrt(int x) {
        int l = 0, r = x;
        int rightBound = -1;
        while(l <= r){
            int mid = l+((r-l)>>1);
            long sqrt = (long) mid * mid;
            if(sqrt == x){
                return mid;
            }else if(sqrt < x){
                l = mid+1;
            }else{
                r = mid-1;
                rightBound = r;
            }
        }
        return rightBound;
    }

    @Test
    public void test(){
        System.out.println(mySqrt(5));
        System.out.println(mySqrt(4));
        System.out.println(mySqrt(9));
        System.out.println(mySqrt(8));
        System.out.println(mySqrt(2147395599));
    }
}
